<template>
    <div id="bigbox">
        <div id="box">
        <p class="reg-ip" v-if="all.flg_1">{{ all.esti_user }}</p>
        <p class="reg-pass" v-if="all.flg_2">{{ all.esti_pass }}</p>
        <p class="reg-user" v-if="all.flg_3">{{ all.exist_user }}</p>
        <p class="reg-user" v-if="all.flg_5">{{ all.register_ok }}</p>
        <h1>register 注 册</h1>
        <div class="inp">
            <input type="text" id="user" placeholder="请输入账号" v-model="user_info.user" />
            <br>
            <input type="password" id="pass" placeholder="请输入密码" v-model="user_info.pass" />
            <br>
            <input type="password" id="pass1" placeholder="确认密码" v-model="okPass" />
            <br>
            <div id="code">
                <input type="text" placeholder="验证码" class="input-val" />
                <canvas id="canvas" width="100" height="30px"></canvas>
            </div>
            <button class="reg" @click="register()">注册</button>
            <button class="reg" @click="skip_login()">登录</button>
            <p id="no-code" v-if="all.flg_4">{{ all.code_err }}</p>
        </div>
    </div>
    </div>
</template>
<script scoped>
import { ref } from 'vue'
import { setRegister } from '../api/api.js'
export default {
    name: 'register',
    setup() {
        const user_info = ref({
            user: '',
            pass: ''
        })
        const okPass = ref('')
        const all = ref({
            esti_user: '账 号 或 密 码 不 能 为 空',
            esti_pass: '密 码 确 认 错 误',
            exist_user: '账 号 已 存 在',
            code_err: '验证码错误',
            register_ok: '注 册 成 功',
            flg_1: false,
            flg_2: false,
            flg_3: false,
            flg_4: false,
            flg_5: false
        })
        return {
            user_info,
            okPass,
            all
        }
    },
    methods: {
        register() {
            let timer = null
            if (this.okPass != this.user_info.pass) {
                this.all.flg_2 = true
                timer = setTimeout(() => {
                    this.all.flg_2 = false
                }, 1500)
            } else if (this.user_info.pass == '' && this.user_info.user == '') {
                this.all.flg_1 = true
                timer = setTimeout(() => {
                    this.all.flg_1 = false
                }, 1500)
            } else {
                const _data = this.user_info
                console.log(111)
                setRegister(_data)
                .then(res => {
                        console.log(res)
                        if (res.data.code == 2) {
                            this.all.flg_3 = true
                            timer = setTimeout(() => {
                                this.all.flg_3 = false
                            }, 1500)
                        } else if (res.data.code == 1) {
                            this.all.flg_5 = true
                            timer = setTimeout(() => {
                                this.all.flg_5 = false
                            }, 1500)
                            this.user_info.pass = this.user_info.user = this.okPass = ''
                        }
                })
                .catch(err => {
                    console.log(err)
                })
            }

        },
        skip_login() {
            this.$router.push('/login')
        }
    }
}
</script>
<style scoped>
* {
    margin: 0;
    padding: 0;
    outline: none;
}

input::-webkit-input-placeholder {
    color: rgba(255, 255, 255, 0.8);
    font-size: 16px;
}

#bigbox{
	width: 100%;
	height: calc(100vh - 30px);
	padding-top: 30px;
	background: linear-gradient(to top, #e56567, #9397e1);
}

#box {
    margin: 100px auto;
    width: 380px;
    height: 400px;
    border-radius: 8px;
    background-color: #0c132c;
    box-shadow: 0 20px 20px rgba(12, 19, 44, .8);
    overflow: hidden;
}

h1 {
    width: 100%;
    height: 30px;
    text-align: center;
    margin-top: 30px;
    color: #fff;
    font-size: 26px;
}

.inp {
    margin-top: 20px;
    /* width: 100%; */
    text-align: center;
    color: #fff;
    background: rgba(0, 0, 0, 0);
}

.inp>input,
.input-val {
    width: 200px;
    padding: 6px 15px;
    border-radius: 50px;
    height: 20px;
    background-color: rgba(255, 255, 255, .5);
    margin-top: 20px;
    border: 2px solid rgb(61, 143, 199);
    text-align: center;
    line-height: 20px;
    color: #fff;
    font-size: 18px;
}

.reg {
    margin: 30px 10px;
    height: 30px;
    font-size: 18px;
    color: #fff;
    width: 100px;
    border: none;
    background-color: rgba(255, 255, 255, .5);
    border: 2px solid rgb(61, 143, 199);
    border-radius: 50px;
    cursor: pointer;
}

.reg:hover {
    color: #e56567;
}

.reg-pass,
.reg-user,
.reg-ip {
    position: absolute;
    font-size: 14px;
    color: rgba(203, 45, 1, 0.7);
    left: 50%;
    transform: translateX(-50%);
}

.reg-ip {
    color: rgba(203, 45, 1, 0.7);
    font-size: 16px;
    top: 200px;
}

.reg-pass {
    top: 323px;
}

.reg-user {
    top: 210px;
}

#code {
    width: 230px;
    left: 50%;
    transform: translateX(-50%);
    position: relative;
    height: 35px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 20px;
}

#canvas {
    border: 1px solid #ccc;
    border-radius: 5px;
    cursor: pointer;
}

.input-val {
    width: 80px;
    margin-top: 0px;
}

#no-code {
    position: absolute;
    top: 51.6%;
    left: 48%;
    font-size: 14px;
    color: rgba(203, 45, 1, 0.7);
}
</style>